<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use app\Request;
use app\repositories\contracts\AdminAuthInterface;

class Login extends BaseController
{
    /***
     * @return string
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 14:58
     * @Description 后台登陆界面
     */
    public function index(Request $request,$errorMsg = '')
    {
        //如果已登录则跳转到首页
        if(!empty($request->rootData)){
            return redirect('/admin/index');
        }
        return View::fetch('index',['errorMsg'=>$errorMsg]);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 14:58
     * @Description 用户名密码检测
     */
    public function CheckRoot(Request $request,AdminAuthInterface $auth)
    {
        if(!$request->isPost()){
            return View::fetch('index');
        }
        $loginData = $request->param();
        $result = $auth->checkLogin($loginData);
        return layuiJson($result['code'],$result['msg'], $result['data']);
    }

    /***
     * @return string|\think\response\Json
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 16:48
     * @Description 退出登录
     */
    public function Logout()
    {
        session('RootData', null);
        return redirect('index');
    }
}
